package com.ezuikit.open;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.ezvizuikit.open.EZUIKit;
import com.ezvizuikit.open.EZUIPlayer;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZConstants.EZPTZAction;
import com.videogo.openapi.EZConstants.EZPTZCommand;
import com.videogo.util.LogUtil;

import android.text.TextUtils;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import com.videogo.openapi.EZPlayer;
import android.content.Intent;
import android.graphics.Bitmap;

import android.view.View.OnTouchListener;
 import android.os.Environment;
import com.videogo.exception.InnerException;
import android.view.Window;


 

public class EzvizActivity extends Activity{

   
    private String mAppKey;
    private String accessToken;   
    private String DeviceSerial;
    private int CameraNo;
    
    private PTZcontrol play;
    

    private Handler mHandler = null;
    private EZPlayer mEZPlayer;
    private SurfaceView mRealPlaySv = null;
    private SurfaceHolder mRealPlaySh = null;

    private ImageButton ptzTopBtn;
    private ImageButton ptzBottomBtn;
    private ImageButton ptzLeftBtn;
    private ImageButton ptzRightBtn;
    private ImageButton ptzFlipBtn;
    private ImageButton enlarge;
     private ImageButton narrow;

    

    

 private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case 1:
        String info = (String) msg.obj;
         System.out.println(info);
          Toast.makeText(getApplicationContext(),info, 0).show();
         break;
      case 0:
        Toast.makeText(getApplicationContext(), "连接网络失败...", 0).show();
        break;
      }
    }
  };
 

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
        //         WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 第一种方法
        setContentView(getId("activity_main","layout"));
         
        
        init();
        start();
        

        
         
     }

     public void init(){
         mRealPlaySv = (SurfaceView) findViewById(getIdTypeId("realplay_sv"));
         mRealPlaySh = mRealPlaySv.getHolder();
         
         mHandler = new Handler();
         play=new PTZcontrol();

         String cameraNo= this.getIntent().getStringExtra("cameraNo");
         CameraNo = Integer.parseInt(cameraNo);   
         DeviceSerial= this.getIntent().getStringExtra("deviceSerial");

        mAppKey= this.getIntent().getStringExtra("mAppKey");
        accessToken= this.getIntent().getStringExtra("mAccessToken");

         ptzTopBtn = (ImageButton) findViewById(getIdTypeId("ptz_top_btn"));
         ptzTopBtn.setOnTouchListener(mOnTouchListener);

         ptzBottomBtn = (ImageButton)findViewById(getIdTypeId("ptz_bottom_btn"));
         ptzBottomBtn.setOnTouchListener(mOnTouchListener);

         ptzLeftBtn = (ImageButton) findViewById(getIdTypeId("ptz_left_btn"));
         ptzLeftBtn.setOnTouchListener(mOnTouchListener);

         ptzRightBtn = (ImageButton)findViewById(getIdTypeId("ptz_right_btn"));
         ptzRightBtn.setOnTouchListener(mOnTouchListener);

         ptzFlipBtn = (ImageButton) findViewById(getIdTypeId("ptz_flip_btn"));
         ptzFlipBtn.setOnTouchListener(mOnTouchListener);

         enlarge = (ImageButton) findViewById(getIdTypeId("bt_enlarge"));
         enlarge.setOnTouchListener(mOnTouchListener);

         narrow = (ImageButton) findViewById(getIdTypeId("bt_narrow"));
         narrow.setOnTouchListener(mOnTouchListener);
     }


   private OnTouchListener mOnTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionevent) {
            boolean ptz_result = false;
            int action = motionevent.getAction();
            final int speed = EZConstants.PTZ_SPEED_DEFAULT;
            switch (action) {
                case MotionEvent.ACTION_DOWN:

                if(view.getId()==getIdTypeId("ptz_top_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 1);
                }else if(view.getId()==getIdTypeId("ptz_bottom_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 2);
                }
                 
                else if(view.getId()==getIdTypeId("ptz_left_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 3);
                }

                else if(view.getId()==getIdTypeId("ptz_right_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 4);
                }
                else if(view.getId()==getIdTypeId("bt_enlarge")){
                   play.control(accessToken,DeviceSerial,CameraNo, 9);
                }
                else if(view.getId()==getIdTypeId("bt_narrow")){
                   play.control(accessToken,DeviceSerial,CameraNo, 10);
                }
                   
                    break;
                case MotionEvent.ACTION_UP:
                 if(view.getId()==getIdTypeId("ptz_top_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 8);
                }else if(view.getId()==getIdTypeId("ptz_bottom_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 8);
                }
                 
                else if(view.getId()==getIdTypeId("ptz_left_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 8);
                }

                else if(view.getId()==getIdTypeId("ptz_right_btn")){
                   play.control(accessToken,DeviceSerial,CameraNo, 8);
                }
                else if(view.getId()==getIdTypeId("bt_enlarge")){
                   play.control(accessToken,DeviceSerial,CameraNo, 8);
                }
                else if(view.getId()==getIdTypeId("bt_narrow")){
                   play.control(accessToken,DeviceSerial,CameraNo, 8);
                }
                  
                    break;
                default:
                    break;
            }
            return false;
        }
    };
    
   
    public void play(View view){
      start();
    }


    public void stop(View view) {
        mEZPlayer.stopRealPlay();
    }
   
   public void GetPicture(View view){
     
        if (mEZPlayer != null) {
          Thread thr = new Thread() {
                @Override
                public void run() {
                    Bitmap bmp = mEZPlayer.capturePicture();
                    if (bmp != null) {
                        try {
                           java.util.Date date = new java.util.Date();
                            final String path = Environment.getExternalStorageDirectory().getPath() + "/hkvision/CapturePicture/" + String.format("%tY", date)
                                    + String.format("%tm", date) + String.format("%td", date) + "/" + String.format("%tH", date) + String.format("%tM", date) + String.format("%tS", date)  +".jpg";

                            if (TextUtils.isEmpty(path)) {
                                bmp.recycle();
                                bmp = null;
                                return;
                            }
                            
                            play.saveCapturePictrue(path, bmp);
                            Message msg = new Message();
                            msg.what = 1;
                            msg.obj = "图片已保存至"+path;
                            handler.sendMessage(msg);
                            
                       } catch (InnerException e) {
                            e.printStackTrace();
                            Message msg = new Message();
                            msg.what = 0;
                            handler.sendMessage(msg);
                        } finally {
                            if (bmp != null) {
                                bmp.recycle();
                                bmp = null;
                                return;
                            }
                        }
                    }
                    super.run();
                }
            };
            thr.start();
        }
     }


 
    public void start(){
      mEZPlayer = EZOpenSDK.getInstance().createPlayer(DeviceSerial, CameraNo);
        mEZPlayer.setHandler(mHandler);
        mEZPlayer.setSurfaceHold(mRealPlaySh);
        mEZPlayer.startRealPlay();
      
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
         System.exit(0);
    }


    private int getIdTypeLayout(String name){
        return getId(name,"layout");
    }
    private int getIdTypeId(String name){
        return getId(name,"id");
    }
    private int getId(String idName, String type){
        return getResources().getIdentifier(idName, type,getPackageName());
    }
}
